package com.venusic.handwritedemo.slice;

import com.venusic.handwritedemo.ResourceTable;
import com.venusic.handwrite.view.HandWriteView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Environment;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

public class SecondAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);

        final boolean isCrop = getAbility().getIntent().getBooleanParam("isCrop", false);

        // 获取屏幕分辨率
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
        Point pt = new Point();
        display.get().getSize(pt);
        float pointY = pt.getPointY();

        HandWriteView hwView = (HandWriteView) findComponentById(ResourceTable.Id_handwriting);



        //保存
        Button btn_save = (Button) findComponentById(ResourceTable.Id_save);
        btn_save.setClickedListener(component -> {
            if (hwView.isSign())
                try {
                    if (isCrop) {
                        String path=getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()
                                + File.separator + "ls.jpeg";
//                        hwView.save(path, true, 10, true,this);
                        hwView.save(path,true,10,false,this);
                        Intent intent1 = new Intent();
                        intent1.setParam("path",path);
                        getAbility().setResult(101,intent1);
                        getAbility().terminateAbility();
                    } else {
                        String path=getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()
                                + File.separator + "qm.jpeg";
                        hwView.save(path,this);
                        Intent intent2 = new Intent();
                        intent2.setParam("path",path);
                        getAbility().setResult(100, intent2);
                        getAbility().terminateAbility();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            else {
                ToastDialog toastDialog = new ToastDialog(this);
                toastDialog.setText("还没有签名！").setDuration(5000).show();
            }
        });

        //清屏
        Button btn_clear = (Button) findComponentById(ResourceTable.Id_clear);
        btn_clear.setClickedListener(component -> {
            hwView.clear();
        });

        //改变颜色
        Button btn_cColor = (Button) findComponentById(ResourceTable.Id_change_color);
        btn_cColor.setClickedListener(component -> {
                hwView.setPaintColor(Color.RED);
        });

        //改变宽度
        Button btn_cWidth = (Button) findComponentById(ResourceTable.Id_change_width);
        btn_cWidth.setClickedListener(component -> {
                hwView.setPaintWidth(5, 20);
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
